package org.arl.modem.transportARQ;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;

class FetchLogs {
   public static void main(String args[]) {
      List<Long> SenderTx = new LinkedList<Long>();
      List<Long> SenderRx = new LinkedList<Long>();
      try {
         FileInputStream fstream = new FileInputStream(
         // "/home/rohit/Desktop/arl/mod1/modem-sim/logs/log-0.txt");
         // "/home/rohit/Desktop/arl/mod1/txlogs.txt");
               "/home/rohit/Desktop/logs/tx6.txt");
         // "/home/rohit/Downloads/tx2.txt");
         DataInputStream in = new DataInputStream(fstream);
         BufferedReader br = new BufferedReader(new InputStreamReader(in));
         // FileWriter outFilestx = new
         // FileWriter("/home/rohit/Desktop/sendertx");
         // PrintWriter outstx = new PrintWriter(outFilestx);
         FileWriter outFilesrx = new FileWriter(
         // "/home/rohit/Desktop/txsim3_abs.txt");
               "/home/rohit/Desktop/logsa/tx6a.txt");
         PrintWriter outsrx = new PrintWriter(outFilesrx);
         long l1 = 0;
         String strLine;
         while ((strLine = br.readLine()) != null) {
            // Print the content on the console
            if (strLine.contains(".transportARQ.")) {
               if (strLine.contains("$$$")) {
                  // String s1 = strLine.split(".transportARQ.")[1];
                  // System.out.println(s1);
                  // String s2[] = s1.split("FINE|$$$");
                  // for (String d : s2)
                  // System.out.println(d + ":");
                  // String s = s2[1].split(":")[1];
                  // System.out.println(s);
                  String s = strLine.split(".transportARQ.")[1]
                        .split("FINE|$$$")[1].split(":")[0];
                  // System.out.println(s);
                  if (l1 == 0)
                     l1 = Long.parseLong(strLine.split(".transportARQ.")[1]
                           .split("FINE|$$$")[1].split(":")[1]);
                  long temp = Long.parseLong(strLine.split(".transportARQ.")[1]
                        .split("FINE|$$$")[1].split(":")[1]) - l1;
                  l1 = Long.parseLong(strLine.split(".transportARQ.")[1]
                        .split("FINE|$$$")[1].split(":")[1]);

                  System.out.println(s + ": " + temp);
                  outsrx.println(s + ": " + temp);

               }
            }

         }
         // outstx.close();
         outsrx.close();
         in.close();
         l1 = 0;
         FileInputStream fstream2 = new FileInputStream(
         // "/home/rohit/Desktop/arl/mod2/modem-sim/logs/log-0.txt");
         // "/home/rohit/Desktop/arl/mod1/rxlogs.txt");
               "/home/rohit/Desktop/logs/rx6.txt");
         // "/home/rohit/Desktop/modemlogs/rx_rohit");
         DataInputStream in2 = new DataInputStream(fstream2);
         BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
         FileWriter outFilerrx = new FileWriter(
         // "/home/rohit/Desktop/rxsim3_abs.txt");
               "/home/rohit/Desktop/logsa/rx6a.txt");
         PrintWriter outrrx = new PrintWriter(outFilerrx);

         String strLine2;
         while ((strLine2 = br2.readLine()) != null) {
            // Print the content on the console
            if (strLine2.contains(".transportARQ.")) {
               if (strLine2.contains("$$$")) {
                  String s = strLine2.split(".transportARQ.")[1]
                        .split("FINE|$$$")[1].split(":")[0];
                  if (l1 == 0)
                     l1 = Long.parseLong(strLine2.split(".transportARQ.")[1]
                           .split("FINE|$$$")[1].split(":")[1]);
                  long temp = Long
                        .parseLong(strLine2.split(".transportARQ.")[1]
                              .split("FINE|$$$")[1].split(":")[1])
                        - l1;
                  l1 = Long.parseLong(strLine2.split(".transportARQ.")[1]
                        .split("FINE|$$$")[1].split(":")[1]);

                  System.out.println(s + ": " + temp);
                  outrrx.println(s + ": " + temp);

               }
            }
         }
         outrrx.close();
         // outrtx.close();
         in2.close();
      } catch (Exception e) {// Catch exception if any
         System.err.println("Error: " + e.getMessage());
      }
   }
}
